package com.alks.function.service.impl.biocontrol.deptwork;

import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.entity.data.entity.CpLastInfo;
import com.alks.entity.data.entity.CpLastSize;
import com.alks.entity.data.entity.CpLastStyle;
import com.alks.entity.data.entity.SdStyle;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.biocontrol.deptwork.LastInfoDto;
import com.alks.function.mapper.SdStyleMapper;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_CpLastInfoMapper;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_CpLastSizeMapper;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_CpLastStyleMapper;
import com.alks.function.service.biocontrol.deptwork.ICpLastService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.alks.entity.data.enums.ResultCodeEnum.INPUT_ERROR;

@Service
@Slf4j
@RequiredArgsConstructor
public class CpLastServiceImpl extends ServiceImpl<BC_DW_CpLastInfoMapper, CpLastInfo> implements ICpLastService {
    private final BC_DW_CpLastInfoMapper infoMapper;
    private final BC_DW_CpLastStyleMapper styleMapper;
    private final BC_DW_CpLastSizeMapper sizeMapper;
    private final SdStyleMapper sdStyleMapper;

    @Override
    public ResponseInfo getLastInfoQuery(String lastNo, String style) {
        /*获取数据*/
        List<LastInfoDto> dtos = infoMapper.getLastInfoQuery(lastNo, style, UserIdThread.get().getCompanyId());
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo lastInfoAdd(CpLastInfo request) {
        /*入参合法性检测*/
        if (request.getLastNo() == null) {
            return ResponseInfo.error("楦头不能为空", INPUT_ERROR.getCode());
        }
        Integer count = lambdaQuery()
                .eq(CpLastInfo::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastInfo::getLastNo, request.getLastNo()).count();
        if (count > 0) {
            return ResponseInfo.error("楦头不能重复", INPUT_ERROR.getCode());
        }
        /*补全并写入数据*/
        request.setCompanyId(UserIdThread.get().getCompanyId());
        infoMapper.insert(request);
        return ResponseInfo.ok();
    }

    @Override
    @Transactional
    public ResponseInfo lastInfoUpdate(CpLastInfo request, String lastNoOld) {
        /*入参合法性检测*/
        if (request.getLastNo() == null) {
            return ResponseInfo.error("楦头不能为空", INPUT_ERROR.getCode());
        }
        if (!request.getLastNo().equals(lastNoOld)) {
            Integer count = lambdaQuery()
                    .eq(CpLastInfo::getCompanyId, UserIdThread.get().getCompanyId())
                    .eq(CpLastInfo::getLastNo, request.getLastNo()).count();
            if (count > 0) {
                return ResponseInfo.error("楦头不能重复", INPUT_ERROR.getCode());
            }
            /*修改子表楦头号*/
            LambdaUpdateWrapper<CpLastSize> sizeWrapper = new LambdaUpdateWrapper<CpLastSize>()
                    .eq(CpLastSize::getCompanyId, UserIdThread.get().getCompanyId())
                    .eq(CpLastSize::getLastNo, lastNoOld)
                    .set(CpLastSize::getLastNo, request.getLastNo());
            sizeMapper.update(null, sizeWrapper);
            LambdaUpdateWrapper<CpLastStyle> styleWrapper = new LambdaUpdateWrapper<CpLastStyle>()
                    .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                    .eq(CpLastStyle::getLastNo, lastNoOld)
                    .set(CpLastStyle::getLastNo, request.getLastNo());
            styleMapper.update(null, styleWrapper);
        }
        /*补全并更新主表数据*/
        LambdaUpdateWrapper<CpLastInfo> wrapper = new LambdaUpdateWrapper<CpLastInfo>()
                .eq(CpLastInfo::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastInfo::getLastNo, lastNoOld)
                .set(request.getTech1() == null, CpLastInfo::getTech1, null)
                .set(request.getTech2() == null, CpLastInfo::getTech2, null)
                .set(request.getTech3() == null, CpLastInfo::getTech3, null)
                .set(request.getTech4() == null, CpLastInfo::getTech4, null)
                .set(request.getTech5() == null, CpLastInfo::getTech5, null)
                .set(request.getTech6() == null, CpLastInfo::getTech6, null)
                .set(request.getTech7() == null, CpLastInfo::getTech7, null)
                .set(request.getTech8() == null, CpLastInfo::getTech8, null);
        infoMapper.update(request, wrapper);
        return ResponseInfo.ok();
    }

    @Override
    @Transactional
    public ResponseInfo lastInfoDelete(String lastNo) {
        /*删除三张表数据*/
        LambdaUpdateWrapper<CpLastStyle> styleWrapper = new LambdaUpdateWrapper<CpLastStyle>()
                .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastStyle::getLastNo, lastNo);
        styleMapper.delete(styleWrapper);

        LambdaUpdateWrapper<CpLastSize> sizeWrapper = new LambdaUpdateWrapper<CpLastSize>()
                .eq(CpLastSize::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastSize::getLastNo, lastNo);
        sizeMapper.delete(sizeWrapper);

        LambdaQueryWrapper<CpLastInfo> wrapper = new LambdaQueryWrapper<CpLastInfo>()
                .eq(CpLastInfo::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastInfo::getLastNo, lastNo);
        infoMapper.delete(wrapper);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getLastStyleQuery(String lastNo) {
        /*查询数据*/
        LambdaUpdateWrapper<CpLastStyle> styleWrapper = new LambdaUpdateWrapper<CpLastStyle>()
                .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastStyle::getLastNo, lastNo);
        List<CpLastStyle> dtos = styleMapper.selectList(styleWrapper);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo lastStyleAdd(List<String> style, String lastNo) {
        /*判断入参合法性*/
        if (style == null || style.isEmpty() || lastNo == null || lastNo.isEmpty()) {
            return ResponseInfo.error("楦头和型体不能为空", INPUT_ERROR.getCode());
        }
        LambdaQueryWrapper<SdStyle> wrapper = new LambdaQueryWrapper<SdStyle>()
                .select(SdStyle::getStyleNo)
                .eq(SdStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .in(SdStyle::getStyleNo, style);
        List<String> style1 = sdStyleMapper.selectList(wrapper)         //获取真实存在的型体集合
                .stream().map(SdStyle::getStyleNo).collect(Collectors.toList());
        if (style.size() == 1 && style1.size() < 1) {                      //只有一个型体的时候判断是否存在，否则不做提示，直接添加有效的型体
            return ResponseInfo.error("型体未建档");
        }
        style.retainAll(style1);               //保留有效型体
        LambdaQueryWrapper<CpLastStyle> styleWrapper = new LambdaQueryWrapper<CpLastStyle>()
                .select(CpLastStyle::getStyle)
                .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastStyle::getLastNo, lastNo)
                .in(CpLastStyle::getStyle, style);
        List<String> style2 = styleMapper.selectList(styleWrapper)              //找出重复的型体
                .stream().map(CpLastStyle::getStyle).collect(Collectors.toList());
        if (style.size() == 1 && style2.size() > 0) {
            return ResponseInfo.error("型体已存在", INPUT_ERROR.getCode());
        }
        style.removeAll(style2);               //去除重复型体
        /*写入数据*/
        CpLastStyle lastStyle = new CpLastStyle();
        lastStyle.setCompanyId(UserIdThread.get().getCompanyId());
        for (String s : style) {
            lastStyle.setStyle(s);
            styleMapper.insert(lastStyle);
        }
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo lastStyleUpdate(CpLastStyle request, String styleOld) {
        /*判断入参合法性*/
        if (request.getLastNo() == null || request.getLastNo().isEmpty()) {
            return ResponseInfo.error("楦头不能为空", INPUT_ERROR.getCode());
        }
        if (request.getStyle() == null || request.getStyle().isEmpty()) {     //形体为空则直接删除
            LambdaQueryWrapper<CpLastStyle> styleWrapper = new LambdaQueryWrapper<CpLastStyle>()
                    .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                    .eq(CpLastStyle::getLastNo, request.getLastNo())
                    .eq(CpLastStyle::getStyle, styleOld);
            styleMapper.delete(styleWrapper);
            return ResponseInfo.ok();
        }
        if (request.getStyle().equals(styleOld)) {
            return ResponseInfo.ok();
        }
        LambdaQueryWrapper<SdStyle> sdWrapper = new LambdaQueryWrapper<SdStyle>()
                .eq(SdStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(SdStyle::getStyleNo, request.getStyle());
        Integer count = sdStyleMapper.selectCount(sdWrapper);
        if (count == 0) {
            return ResponseInfo.error("型体未建档", INPUT_ERROR.getCode());
        }
        LambdaQueryWrapper<CpLastStyle> styleWrapper = new LambdaQueryWrapper<CpLastStyle>()
                .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastStyle::getLastNo, request.getLastNo())
                .eq(CpLastStyle::getStyle, request.getStyle());
        Integer count1 = styleMapper.selectCount(styleWrapper);
        if (count1 > 0) {
            return ResponseInfo.error("型体已存在", INPUT_ERROR.getCode());
        }
        /*更新数据*/
        LambdaQueryWrapper<CpLastStyle> wrapper = new LambdaQueryWrapper<CpLastStyle>()
                .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastStyle::getLastNo, request.getLastNo())
                .eq(CpLastStyle::getStyle, styleOld);
        request.setCompanyId(UserIdThread.get().getCompanyId());
        styleMapper.update(request, styleWrapper);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo lastStyleDelete(String lastNo, String style) {
        /*删除数据*/
        LambdaQueryWrapper<CpLastStyle> styleWrapper = new LambdaQueryWrapper<CpLastStyle>()
                .eq(CpLastStyle::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastStyle::getLastNo, lastNo)
                .eq(CpLastStyle::getStyle, style);
        styleMapper.delete(styleWrapper);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getLastSizeQuery(String lastNo) {
        /*获取数据*/
        LambdaQueryWrapper<CpLastSize> sizeWrapper = new LambdaQueryWrapper<CpLastSize>()
                .eq(CpLastSize::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastSize::getLastNo, lastNo);
        List<CpLastSize> dtos = sizeMapper.selectList(sizeWrapper);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo lastSizeDelete(String lastNo, String seqNo) {
        /*获取数据*/
        LambdaQueryWrapper<CpLastSize> sizeWrapper = new LambdaQueryWrapper<CpLastSize>()
                .eq(CpLastSize::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpLastSize::getLastNo, lastNo)
                .eq(CpLastSize::getSeqNo, seqNo);
        sizeMapper.delete(sizeWrapper);
        return ResponseInfo.ok();
    }

    @Override
    public void lastNoValid(String lastNo) {
        /*判断是否存在*/
        LambdaQueryWrapper<CpLastInfo> wrapper = new LambdaQueryWrapper<CpLastInfo>()
                .eq(CpLastInfo::getCompanyId, UserIdThread.get().getCompanyId())
                .in(CpLastInfo::getLastNo, lastNo);
        if (infoMapper.selectCount(wrapper) == 0){
            throw new ServiceErrorException("楦头号不存在");
        }
    }

    @Override
    public void lastNoValid (List<String> lastNoList) {
        /*不处理为空*/
        if (ArrayUtils.isNoEmpyt(lastNoList)){
            return;
        }
        Set<String> cutNoSet = new HashSet<>(lastNoList);
        /*判断是否存在*/
        LambdaQueryWrapper<CpLastInfo> wrapper = new LambdaQueryWrapper<CpLastInfo>()
                .select(CpLastInfo::getLastNo)
                .eq(CpLastInfo::getCompanyId, UserIdThread.get().getCompanyId())
                .in(CpLastInfo::getLastNo, cutNoSet)
                .groupBy(CpLastInfo::getLastNo);
        List<CpLastInfo> infos = infoMapper.selectList(wrapper);
        if (ArrayUtils.isEmpyt(infos)){
            throw new ServiceErrorException("楦头号不存在");
        }
        /*去重*/
        infos.stream().map(CpLastInfo::getLastNo).collect(Collectors.toList()).forEach(cutNoSet::remove);
        if (ArrayUtils.isNoEmpyt(cutNoSet)){
            /*获取缺少的冲刀号*/
            String emptyCutNo = new ArrayList<>(cutNoSet).get(0);
            throw new ServiceErrorException("楦头号:"+ emptyCutNo +"不存在");
        }
    }
}
